package com.youdianstar.app.ui

import android.os.Handler
import android.os.Looper
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen

import com.youdianstar.app.BR
import com.youdianstar.app.R
import com.youdianstar.app.databinding.ActivitySplashBinding
import com.youdianstar.app.ui.account.LoginActivity
import com.youdianstar.app.common.base.BaseActivity

import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.common.util.Tool
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.util.LogUtil
import com.youdianstar.app.common.auko.startActivity
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: SplashActivity
 * Author: 82149 Create: 2021/9/24 17:18
 * Changes (from 2021/9/24)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class SplashActivity : BaseActivity<ActivitySplashBinding,SplashViewModel>() {
    override fun layoutId(): Int = R.layout.activity_splash

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): SplashViewModel {
        val model : SplashViewModel by viewModel()
        return model
    }

    override fun initContentCreate() {
        super.initContentCreate()
        //Android 12启动屏处理
        val splashScreen = installSplashScreen()
        // Keep the splash screen visible for this Activity.
        splashScreen.setKeepOnScreenCondition { true }
    }

    private val mHandler = Handler(Looper.myLooper()!!)

    override fun initData() {
        super.initData()
        if (StrUtil.isEmpty(DataAppManager.instance.getPhoneId()))
            DataAppManager.instance.setPhoneId(Tool.getAndroidId(this))

        mHandler.postDelayed(Runnable {
            startView()
            finish()
        }, 500L)

        checkSchemeIntentAction()
    }

    override fun initClickEvent() {}

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }

    private fun startView(){
        if (DataAppManager.instance.isShowGuideImg()){
            if (DataAppManager.instance.isTokenExist()){
                startActivity<MainActivity>()
            } else {
                startActivity<LoginActivity>()
            }
        }else{
            startActivity<GuideActivity>()
        }
    }

    /**
     * 处理 URL scheme
     * 例: youdianstar://app/splash?invitepartner=xxx
     *  youdianstar://app/splash?invitefriend=xx&brandId=xx&brandName=xx
     */
    private fun checkSchemeIntentAction(){
        val uri = intent.data
        if (null!=uri && uri.pathSegments!=null){
            LogUtil.d("---url = $uri")
            val partnerId :String = uri.getQueryParameter("invitepartner")?:""
            if (!StrUtil.isEmpty(partnerId)){
                DataAppManager.instance.partnerLinkId = partnerId
                return
            }
            val friendId :String = uri.getQueryParameter("invitefriend")?:""
            if (!StrUtil.isEmpty(friendId)){
                DataAppManager.instance.friendLinkID = friendId
                return
            }
            //Scan Reward Code
            val rewardType :String = uri.getQueryParameter("type")?:""
            val rewardCode :String = uri.getQueryParameter("code")?:""
            if (!StrUtil.isEmpty(rewardType) && !StrUtil.isEmpty(rewardCode)){
                DataAppManager.instance.rewardTypeTx = rewardType
                DataAppManager.instance.rewardCodeTx = rewardCode
            }
        }
    }
}